package com.atguigu.flink.chapter07.state;


import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.apache.flink.streaming.connectors.kafka.KafkaSerializationSchema;
import org.apache.flink.util.Collector;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;


import javax.annotation.Nullable;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class Flink11_Kafka_Flink_Kafka {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        //设置环境变量，用于登录hdfs 的账户
        System.setProperty("HADOOP_USER_NAME","atguigu");

        env.enableCheckpointing(2000);
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/ck2");

        // 每 1000ms 开始一次 checkpoint
        env.enableCheckpointing(1000);
        // 高级选项：
        // 设置模式为精确一次 (这是默认值)
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);

        // 确认 checkpoints 之间的时间会进行 500 ms
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);

        // Checkpoint 必须在一分钟内完成，否则就会被抛弃
        env.getCheckpointConfig().setCheckpointTimeout(60000);

        // 同一时间只允许一个 checkpoint 进行
        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);

        // 开启在 job 中止后仍然保留的 externalized checkpoints
        env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);

        Properties sourceProp = new Properties();
        sourceProp.put("bootstrap.servers","hadoop162:9092");
        sourceProp.put("group.id","Flink11_Kafka_Flink_Kafka");
        sourceProp.put("auto.offset.reset","latest");

        //数据隔离等级  防止读取别人未提交得数据
        sourceProp.put("isolation.level","read_committed");

        Properties sinkProp = new Properties();
        sinkProp.put("bootstrap.servers","hadoop162:9092");
        sinkProp.put("transaction.timeout.ms",15 * 60 * 1000);


        SingleOutputStreamOperator<Tuple2<String, Long>> result = env.addSource(new FlinkKafkaConsumer<String>("s1", new SimpleStringSchema(), sourceProp))
                .flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {
                    @Override
                    public void flatMap(String value,
                                        Collector<Tuple2<String, Long>> out) throws Exception {
                        for (String s : value.split(" ")) {
                            out.collect(Tuple2.of(s, 1L));
                        }
                    }
                })
                .keyBy(t -> t.f0)
                .sum(1);

        result.addSink(new FlinkKafkaProducer<Tuple2<String, Long>>(
                "default_topic",
                new KafkaSerializationSchema<Tuple2<String, Long>>() {
                    @Override
                    public ProducerRecord<byte[], byte[]> serialize(Tuple2<String, Long> element,
                                                                    @Nullable Long aLong) {
                        return new ProducerRecord<>("s2",(element.f0+"_"+element.f1).getBytes(StandardCharsets.UTF_8));
                    }
                },sinkProp,
                FlinkKafkaProducer.Semantic.EXACTLY_ONCE
        ));

        result.addSink(new SinkFunction<Tuple2<String, Long>>() {
            @Override
            public void invoke(Tuple2<String, Long> value) throws Exception {
                if (value.f0.contains("x")) {
                    throw new RuntimeException("故意跑一个异常");
                }
            }
        });


        env.execute();

    }
}
